package org.dromara.knowledge.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.knowledge.domain.KnowledgeFragment;
import org.dromara.knowledge.domain.bo.KnowledgeFragmentBo;
import org.dromara.knowledge.domain.vo.KnowledgeFragmentVo;
import org.dromara.knowledge.mapper.KnowledgeFragmentMapper;
import org.dromara.knowledge.service.KnowledgeFragmentService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

/**
 * 知识库片段Service业务层处理
 *
 * @author ruoyi
 */
@RequiredArgsConstructor
@Service
public class KnowledgeFragmentServiceImpl extends ServiceImpl<KnowledgeFragmentMapper, KnowledgeFragment> implements KnowledgeFragmentService {

    private final KnowledgeFragmentMapper baseMapper;

    /**
     * 查询知识库片段
     */
    @Override
    public KnowledgeFragmentVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询知识库片段列表
     */
    @Override
    public TableDataInfo<KnowledgeFragmentVo> queryPageList(KnowledgeFragmentBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<KnowledgeFragment> lqw = buildQueryWrapper(bo);
        Page<KnowledgeFragmentVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询知识库片段列表
     */
    @Override
    public List<KnowledgeFragmentVo> queryList(KnowledgeFragmentBo bo) {
        LambdaQueryWrapper<KnowledgeFragment> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<KnowledgeFragment> buildQueryWrapper(KnowledgeFragmentBo bo) {
        LambdaQueryWrapper<KnowledgeFragment> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getKnowledgeId()), KnowledgeFragment::getKnowledgeId, bo.getKnowledgeId());
        lqw.eq(StringUtils.isNotBlank(bo.getDocId()), KnowledgeFragment::getDocId, bo.getDocId());
        lqw.eq(StringUtils.isNotBlank(bo.getFid()), KnowledgeFragment::getFid, bo.getFid());
        lqw.eq(ObjectUtil.isNotNull(bo.getIdx()), KnowledgeFragment::getIdx, bo.getIdx());
        lqw.like(StringUtils.isNotBlank(bo.getContent()), KnowledgeFragment::getContent, bo.getContent());
        return lqw;
    }

    /**
     * 新增知识库片段
     */
    @Override
    public Boolean insertByBo(KnowledgeFragmentBo bo) {
        KnowledgeFragment add = BeanUtil.toBean(bo, KnowledgeFragment.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改知识库片段
     */
    @Override
    public Boolean updateByBo(KnowledgeFragmentBo bo) {
        KnowledgeFragment update = BeanUtil.toBean(bo, KnowledgeFragment.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(KnowledgeFragment entity) {
        // TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除知识库片段
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            // TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    /**
     * 根据知识库ID查询片段列表
     */
    @Override
    public List<KnowledgeFragmentVo> queryListByKnowledgeId(Long knowledgeId) {
        LambdaQueryWrapper<KnowledgeFragment> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeFragment::getKnowledgeId, knowledgeId);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据文档ID查询片段列表
     */
    @Override
    public List<KnowledgeFragmentVo> queryListByDocId(String docId) {
        LambdaQueryWrapper<KnowledgeFragment> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeFragment::getDocId, docId);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 根据片段ID查询片段信息
     */
    @Override
    public KnowledgeFragmentVo queryByFid(String fid) {
        LambdaQueryWrapper<KnowledgeFragment> lqw = Wrappers.lambdaQuery();
        lqw.eq(KnowledgeFragment::getFid, fid);
        return baseMapper.selectVoOne(lqw);
    }
}